local xuanyu = fk.CreateSkill{
  name = "xiaobai__xuanyu",
}

Fk:loadTranslationTable{
  ["xiaobai__xuanyu"] = "悬鱼",
  [":xiaobai__xuanyu"] = "当你获得牌后，若你的宝物栏内没有牌，你可以将其中一张非装备牌置入宝物栏；"..
    "否则你可以回复1点体力，然后若此时不是你的回合，则你需将这些牌置入弃牌堆。",

  ["#xiaobai__xuanyu_recover"] = "悬鱼：你可以回复1点体力 %arg",
  ["#xiaobai__xuanyu_outsideturn"] = "，然后须将此次获得的牌置入弃牌堆",

  ["#xiaobai__xuanyu_put"] = "悬鱼：你可以将其中一张牌置入宝物栏",
  [":xiaobai__xuanyu_treasure"] = "悬鱼宝物",
}

xuanyu:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xuanyu.name) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).type ~= Card.TypeEquip then
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
    end
    if player:getEquipment(Card.SubtypeTreasure) and player.hp ~= player.maxHp then
      event:setCostData(self, cards)
      return player.room:askToSkillInvoke(player, {skill_name = xuanyu.name, promp = "#xiaobai__xuanyu_recover:::"..(room.current == player and "" or "#xiaobai__xuanyu_outsideturn")})
    elseif not player:getEquipment(Card.SubtypeTreasure) then
      room.logic:moveCardsHoldingAreaCheck(cards)
      if #cards > 0 then
        local cds = room:askToCards(player, {
          pattern = ".|.|.|.|.|.|"..table.concat(cards, ','),
          max_num = 1,
          min_num = 1,
          skill_name = xuanyu.name,
          include_equip = false,
          no_indicate = true,
          prompt = "#xiaobai__xuanyu_put"
        })
        if #cds > 0 then
          event:setCostData(self, cds[1])
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getEquipment(Card.SubtypeTreasure) then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = xuanyu.name,
      }
      if player.dead or room.current == player then return end
      room:throwCard(event:getCostData(self), xuanyu.name, player)
    else
      local c = Fk:getCardById(event:getCostData(self))
      room:setCardMark(c, "xiaobai__xuanyu", 1)
      player:filterHandcards()
      room:moveCardIntoEquip(player, c.id, xuanyu.name, false, player)
    end
  end
})

xuanyu:addEffect("filter", {
  card_filter = function (self, card, player, isJudgeEvent)
    return not isJudgeEvent and card:getMark("xiaobai__xuanyu") == 1
  end,
  view_as = function (self, player, card)
    return Fk:cloneCard("xiaobai__xuanyu_treasure", card.suit, card.number)
  end
})

return xuanyu